﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MisClient
{
    public partial class FrmGoods : Form
    {
        private string opState;

        private IBLL.IDepart departBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteDepart();

        private IBLL.IType typeBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteType();

        private IBLL.IShop shopBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteShop();

        private IBLL.IJhType jhTypeBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteJhType();

        private IBLL.IFkType fkTypeBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteFkType();

        private IBLL.IGoods goodsBLL = MisCLL.DAOFactory.CreateInstance().GetRemoteGoods();


        public FrmGoods()
        {
            InitializeComponent();
        }

        private void btnSek_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            btn.FlatAppearance.BorderSize = 0;
            Point point = new Point(btn.Left + this.Left + 5, btn.Top + this.Top + 40);
            contextMenuAssign.Show(point);
        }

        private void ByCodeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string value = InputBox.Input("指定.按编码", "请输入13位商品条码：", "");
            if (!string.IsNullOrEmpty(value))
            {
                if (value.Trim().Length != 13)
                {
                    MessageBox.Show(string.Format("当前商品条码只有{0}位，请重新输入13位的商品条码！", value.Trim().Length), "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                MisEntity.Goods entity = goodsBLL.Load(value);
                if (entity != null)
                {
                    FillValue(entity);
                }
                else
                {
                    MessageBox.Show(string.Format("无记录{0},不可操作!", value), "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void ChoiseListToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string value = InputBox.Input("指定.按编码", "请输入4到6位的商品条码：", "");
            if (!string.IsNullOrEmpty(value))
            {
                if (value.Trim().Length < 4 || value.Trim().Length > 6)
                {
                    MessageBox.Show(string.Format("当前商品条码只有{0}位，请重新输入4到6位的商品条码！", value.Trim().Length), "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                MisEntity.Goods entity = goodsBLL.EndsWithID(value.Trim());
                if (entity != null)
                {
                    FillValue(entity);
                }
                else
                {
                    MessageBox.Show(string.Format("无记录{0},不可操作!", value), "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }


        private void btnAdd_Click(object sender, EventArgs e)
        {

            if (MessageBox.Show("刷新屏幕信息[y/n]？", "提问窗", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                this.txtUnit.Text = "个";
                this.txtUnit_P.Text = "箱";
                this.txtPack.Text = "1";
                this.txtState.Text = "中国";
                this.txtProducter.Text = "公司";
                this.txtTax.Text = "17";
                this.txtPrice.Text = "0";
                this.txtDay11.Text = "366";
                this.txtDayJh.Text = "30";
                this.txtSlMax.Text = "999999";
                this.txtSlMin.Text = "0";
            }
            else
            {
                this.txtUnit.Clear();
                this.txtUnit_P.Clear();
                this.txtPack.Clear();
                this.txtState.Clear();
                this.txtProducter.Clear();
                this.txtTax.Clear();
                this.txtPrice.Clear();
                this.txtDay11.Clear();
                this.txtDayJh.Clear();
                this.txtSlMax.Clear();
                this.txtSlMin.Clear();
            }

            this.txtGoodsDM.Clear();
            this.txtGoodsID.Clear();
            this.txtDate.Text = DateTime.Now.ToShortDateString();
            this.txtSL0.Text = "0";
            this.txtYcsl.Text = "0";
            this.txtYcje.Text = "0";
            this.txtSl2.Text = "0";
            this.txtJe2.Text = "0";
            this.txtSl3.Text = "0";
            this.txtJe3.Text = "0";
            this.txtDate1.Text = DateTime.Now.ToShortDateString();
            this.txtDateLast.Text = DateTime.Now.ToShortDateString();
            this.txtSlLast.Text = "0";
            this.txtJeLast.Text = "0";
            this.txtDate0.Text = DateTime.Now.ToShortDateString();
            this.txtBats.Text = "0";
            this.txtBat.Text = "0";

            this.chkF1.Checked = true;
            this.chkF2.Checked = true;
            this.chkF3.Checked = false;

            this.txtGoodsDM.Enabled = true;
            this.txtGoodsID.Enabled = true;
            this.txtGoods.Enabled = true;
            this.cBoxDep.Enabled = true;
            this.cBoxStore.Enabled = true;
            this.txtUnit.Enabled = true;
            this.txtUnit_P.Enabled = true;
            this.txtPack.Enabled = true;
            this.cBoxType1.Enabled = true;
            this.cBoxType2.Enabled = true;
            this.cBoxType3.Enabled = true;
            this.txtState.Enabled = true;
            this.txtProducter.Enabled = true;
            this.txtBei.Enabled = true;
            this.txtTax.Enabled = true;
            this.txtPrice.Enabled = true;
            this.txtDay11.Enabled = true;
            this.cBoxJhId.Enabled = true;
            this.txtFkId.Enabled = true;
            this.cBoxDayJs.Enabled = true;
            this.txtDayJh.Enabled = true;
            this.txtSlMax.Enabled = true;
            this.txtSlMin.Enabled = true;
            this.chkF1.Enabled = true;
            this.chkF2.Enabled = true;
            this.chkF3.Enabled = true;
            this.opState = "ADD";

            ChangeButton(false);
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnCan_Click(object sender, EventArgs e)
        {
            if (this.opState == "MOD" && !string.IsNullOrEmpty(this.txtGoodsID.Text))
            {
                MisEntity.Goods entity = goodsBLL.Load(this.txtGoodsID.Text.Trim());
                if (entity != null)
                {
                    FillValue(entity);
                }
            }
            else
            {
                this.txtUnit.Clear();
                this.txtUnit_P.Clear();
                this.txtPack.Clear();
                this.txtState.Clear();
                this.txtProducter.Clear();
                this.txtTax.Clear();
                this.txtPrice.Clear();
                this.txtDay11.Clear();
                this.txtDayJh.Clear();
                this.txtSlMax.Clear();
                this.txtSlMin.Clear();
                this.txtGoodsDM.Clear();
                this.txtGoodsID.Clear();
                this.txtDate.Clear();
                this.txtSL0.Clear();
                this.txtYcsl.Clear();
                this.txtYcje.Clear();
                this.txtSl2.Clear();
                this.txtJe2.Clear();
                this.txtSl3.Clear();
                this.txtJe3.Clear();
                this.txtDate1.Clear();
                this.txtDateLast.Clear();
                this.txtSlLast.Clear();
                this.txtJeLast.Clear();
                this.txtDate0.Clear();
                this.txtBats.Clear();
                this.txtBat.Clear();

                this.chkF1.Checked = true;
                this.chkF2.Checked = true;
                this.chkF3.Checked = false;
            }

            this.txtGoodsDM.Enabled = false;
            this.txtGoodsID.Enabled = false;
            this.txtGoods.Enabled = false;
            this.cBoxDep.Enabled = false;
            this.cBoxStore.Enabled = false;
            this.txtUnit.Enabled = false;
            this.txtUnit_P.Enabled = false;
            this.txtPack.Enabled = false;
            this.cBoxType1.Enabled = false;
            this.cBoxType2.Enabled = false;
            this.cBoxType3.Enabled = false;
            this.txtState.Enabled = false;
            this.txtProducter.Enabled = false;
            this.txtBei.Enabled = false;
            this.txtTax.Enabled = false;
            this.txtPrice.Enabled = false;
            this.txtDay11.Enabled = false;
            this.cBoxJhId.Enabled = false;
            this.txtFkId.Enabled = false;
            this.cBoxDayJs.Enabled = false;
            this.txtDayJh.Enabled = false;
            this.txtSlMax.Enabled = false;
            this.txtSlMin.Enabled = false;
            this.chkF1.Enabled = false;
            this.chkF2.Enabled = false;
            this.chkF3.Enabled = false;

            ChangeButton(true);
        }

        private void btnMod_Click(object sender, EventArgs e)
        {
            this.txtGoodsDM.Enabled = true;
            this.txtGoods.Enabled = true;
            this.cBoxDep.Enabled = true;
            this.cBoxStore.Enabled = true;
            this.txtUnit.Enabled = true;
            this.txtUnit_P.Enabled = true;
            this.cBoxType1.Enabled = true;
            this.cBoxType2.Enabled = true;
            this.cBoxType3.Enabled = true;
            this.txtState.Enabled = true;
            this.txtPack.Enabled = true;
            this.txtProducter.Enabled = true;
            this.txtBei.Enabled = true;
            this.txtDay11.Enabled = true;
            this.cBoxJhId.Enabled = true;
            this.txtFkId.Enabled = true;
            this.cBoxDayJs.Enabled = true;
            this.txtDayJh.Enabled = true;
            this.txtSlMax.Enabled = true;
            this.txtSlMin.Enabled = true;
            this.chkF1.Enabled = true;
            this.chkF2.Enabled = true;
            this.chkF3.Enabled = true;
            this.opState = "MOD";
            ChangeButton(false);
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.opState))
            {
                MisEntity.Goods entity = null;
                if (this.txtGoodsDM.Text.Trim().Length == 0 || this.txtGoodsDM.Text.Trim().Length > 6)
                {
                    MessageBox.Show("商品内部编码长度应介于1－6！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtGoodsDM.Focus();
                    return;
                }

                entity = goodsBLL.FindByDM(this.txtGoodsDM.Text.Trim());
                if (entity != null && string.IsNullOrEmpty(entity.DM))
                {
                    MessageBox.Show("商品内部编码重复，请重新填写！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtGoodsDM.Focus();
                    return;
                }

                if (this.txtGoodsID.Text.Trim().Length == 0 || this.txtProducter.Text.Trim().Length > 13)
                {
                    MessageBox.Show("商品编码长度应介于1－13！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtGoodsID.Focus();
                    return;
                }

                if (this.txtGoods.Text.Trim().Length == 0 || this.txtProducter.Text.Trim().Length > 40)
                {
                    MessageBox.Show("品名规格长度应介于1－40！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtGoods.Focus();
                    return;
                }

                if (string.IsNullOrEmpty(this.cBoxDep.Text))
                {
                    MessageBox.Show("部门编码不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.cBoxDep.Focus();
                    return;
                }

                if (string.IsNullOrEmpty(this.txtUnit.Text))
                {
                    MessageBox.Show("数量单位不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtUnit.Focus();
                    return;
                }

                //if (string.IsNullOrEmpty(this.cBoxType1.Text))
                //{
                //    MessageBox.Show("统计分类不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                //    this.cBoxType1.Focus();
                //    return;
                //}

                if (string.IsNullOrEmpty(this.txtUnit_P.Text))
                {
                    MessageBox.Show("外包装不能为空！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.txtUnit_P.Focus();
                    return;
                }


                entity = new MisEntity.Goods();

                entity.DM = this.txtGoodsDM.Text.Trim();
                entity.ID = this.txtGoodsID.Text.Trim();
                entity.GOODS = this.txtGoods.Text.Trim();
                if (this.cBoxDep.SelectedValue != null)
                    entity.DEP_ID = this.cBoxDep.SelectedValue.ToString();
                if (this.cBoxStore.SelectedValue != null)
                    entity.STORE = this.cBoxStore.SelectedValue.ToString();
                entity.UNIT = this.txtUnit.Text.Trim();
                entity.UNIT_P = this.txtUnit_P.Text.Trim();
                entity.PACK = short.Parse(this.txtPack.Text.Trim());
                if (this.cBoxType1.SelectedValue != null)
                    entity.Type1 = this.cBoxType1.SelectedValue.ToString();
                if (this.cBoxType2.SelectedValue != null)
                    entity.Type2 = this.cBoxType2.SelectedValue.ToString();
                if (this.cBoxType3.SelectedValue != null)
                    entity.Type3 = this.cBoxType3.SelectedValue.ToString();
                entity.State = this.txtState.Text.Trim();
                entity.Tax = short.Parse(this.txtTax.Text.Trim());
                entity.Producter = this.txtProducter.Text.Trim();
                entity.PRICE1 = decimal.Parse(this.txtPrice.Text.Trim());
                entity.Price_S = entity.PRICE1;
                entity.Bei = this.txtBei.Text.Trim();
                entity.Day11 = int.Parse(this.txtDay11.Text.Trim());
                if (this.cBoxJhId.SelectedValue != null)
                    entity.Jh_id = this.cBoxJhId.SelectedValue.ToString().ToCharArray()[0];
                entity.Day_jh = int.Parse(this.txtDayJh.Text.Trim());
                entity.Fk_id = this.txtFkId.Text.Trim();
                entity.Sl_max = int.Parse(this.txtSlMax.Text.Trim());
                if (this.cBoxDayJs.SelectedValue != null)
                    entity.DAY_JS = short.Parse(this.cBoxDayJs.SelectedValue.ToString());
                entity.Sl_min = int.Parse(this.txtSlMin.Text.Trim());
                entity.F1 = this.chkF1.Checked ? 'Y' : 'N';
                entity.F2 = this.chkF2.Checked ? 'Y' : 'N';
                entity.F3 = this.chkF3.Checked ? 'Y' : 'N';


                if (this.opState == "ADD")
                {

                    if (goodsBLL.Load(this.txtGoodsID.Text.Trim()) != null)
                    {
                        MessageBox.Show("商品编码重复，请重新填写！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        this.txtGoodsID.Focus();
                        return;
                    }

                    entity.Oper = this.txtOper.Text.Trim();
                    entity.Date = DateTime.Parse(this.txtDate.Text.Trim());
                    entity.SL0 = decimal.Parse(this.txtSL0.Text.Trim());
                    entity.ycsl = decimal.Parse(this.txtYcsl.Text.Trim());
                    entity.ycje = decimal.Parse(this.txtYcje.Text.Trim());
                    entity.SL2 = decimal.Parse(this.txtSl2.Text.Trim());
                    entity.JE2 = decimal.Parse(this.txtJe2.Text.Trim());
                    entity.SL3 = decimal.Parse(this.txtSl3.Text.Trim());
                    entity.JE3 = decimal.Parse(this.txtJe3.Text.Trim());
                    entity.SUPP_ID = this.txtSuppID.Text.Trim();
                    entity.date_1 = DateTime.Parse(this.txtDate1.Text.Trim());
                    entity.date_last = DateTime.Parse(this.txtDateLast.Text.Trim());
                    entity.SL_last = decimal.Parse(this.txtSlLast.Text.Trim());
                    entity.Je_last = decimal.Parse(this.txtJeLast.Text.Trim());
                    entity.date_0 = DateTime.Parse(this.txtDate0.Text.Trim());
                    entity.BATS = short.Parse(this.txtBats.Text.Trim());
                    entity.BAT = short.Parse(this.txtBat.Text.Trim());
                    goodsBLL.Create(entity);
                    MessageBox.Show("本条记录已成功添加！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (this.opState == "MOD")
                {
                    goodsBLL.Update(entity);
                    MessageBox.Show("本条记录已成功修改！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                this.txtGoodsDM.Enabled = false;
                this.txtGoodsID.Enabled = false;
                this.txtGoods.Enabled = false;
                this.cBoxDep.Enabled = false;
                this.cBoxStore.Enabled = false;
                this.txtUnit.Enabled = false;
                this.txtUnit_P.Enabled = false;
                this.txtPack.Enabled = false;
                this.cBoxType1.Enabled = false;
                this.cBoxType2.Enabled = false;
                this.cBoxType3.Enabled = false;
                this.txtState.Enabled = false;
                this.txtProducter.Enabled = false;
                this.txtBei.Enabled = false;
                this.txtTax.Enabled = false;
                this.txtPrice.Enabled = false;
                this.txtDay11.Enabled = false;
                this.cBoxJhId.Enabled = false;
                this.txtFkId.Enabled = false;
                this.cBoxDayJs.Enabled = false;
                this.txtDayJh.Enabled = false;
                this.txtSlMax.Enabled = false;
                this.txtSlMin.Enabled = false;
                this.chkF1.Enabled = false;
                this.chkF2.Enabled = false;
                this.chkF3.Enabled = false;
                this.ChangeButton(true);
            }
        }


        private void btnDel_Click(object sender, EventArgs e)
        {
            MessageBox.Show("不可删除！", "信息窗", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            return;
        }


        private void FrmGoods_Load(object sender, EventArgs e)
        {
            //所属部门初始化
            this.cBoxDep.ValueMember = "DEP_ID";
            this.cBoxDep.DisplayMember = "DEPART";
            this.cBoxDep.DataSource = departBLL.GetAll();
            this.cBoxDep.Text = "";

            //仓库初始化
            this.cBoxStore.ValueMember = "SHOP_ID";
            this.cBoxStore.DisplayMember = "SHOP";
            this.cBoxStore.DataSource = shopBLL.GetAll();
            this.cBoxStore.Text = "";

            //分类一初始化
            this.cBoxType1.ValueMember = "TP1_ID";
            this.cBoxType1.DisplayMember = "TYPE1";
            this.cBoxType1.DataSource = typeBLL.GetType1();
            this.cBoxType1.Text = "";

            //分类二初始化
            this.cBoxType2.ValueMember = "TP2_ID";
            this.cBoxType2.DisplayMember = "TYPE2";
            this.cBoxType2.DataSource = typeBLL.GetType2();
            this.cBoxType2.Text = "";

            //分类三初始化
            this.cBoxType3.ValueMember = "TP3_ID";
            this.cBoxType3.DisplayMember = "TYPE3";
            this.cBoxType3.DataSource = typeBLL.GetType3();
            this.cBoxType3.Text = "";

            //进货类型初始化
            this.cBoxJhId.ValueMember = "JH_ID";
            this.cBoxJhId.DisplayMember = "JHType";
            this.cBoxJhId.DataSource = jhTypeBLL.GetAll();
            this.cBoxJhId.Text = "";

            //结算方式初始化
            this.cBoxDayJs.ValueMember = "FK_id";
            this.cBoxDayJs.DisplayMember = "Fktype";
            this.cBoxDayJs.DataSource = fkTypeBLL.GetAll();
            this.cBoxDayJs.Text = "";
        }

        //填充表单值
        private void FillValue(MisEntity.Goods entity)
        {
            this.txtGoodsDM.Text = entity.DM.Trim();
            this.txtGoodsID.Text = entity.ID.Trim();
            this.txtGoods.Text = entity.GOODS.Trim();
            if (!string.IsNullOrEmpty(entity.DEP_ID))
                this.cBoxDep.SelectedValue = entity.DEP_ID;
            if (!string.IsNullOrEmpty(entity.STORE))
                this.cBoxStore.SelectedValue = entity.STORE;
            this.txtUnit.Text = entity.UNIT;
            this.txtUnit_P.Text = entity.UNIT_P;
            this.txtPack.Text = entity.PACK.ToString();
            this.cBoxType1.SelectedValue = entity.Type1;
            this.cBoxType2.SelectedValue = entity.Type2;
            this.cBoxType3.SelectedValue = entity.Type3;
            this.txtState.Text = entity.State;
            this.txtProducter.Text = entity.Producter.Trim();
            this.txtBei.Text = entity.Bei.Trim();
            this.txtTax.Text = entity.Tax.ToString();
            this.txtPrice.Text = entity.PRICE1.ToString();
            this.txtDay11.Text = entity.Day11.ToString();
            if (entity.Jh_id != null)
                this.cBoxJhId.SelectedValue = entity.Jh_id;
            this.txtFkId.Text = entity.Fk_id;
            if (entity.DAY_JS != null)
                this.cBoxDayJs.SelectedValue = entity.DAY_JS;
            this.txtDayJh.Text = entity.Day_jh.ToString();
            this.txtSlMax.Text = entity.Sl_max.ToString();
            this.txtSlMin.Text = entity.Sl_min.ToString();
            this.chkF1.Checked = entity.F1 == 'Y' ? true : false;
            this.chkF2.Checked = entity.F2 == 'Y' ? true : false;
            this.chkF3.Checked = entity.F3 == 'Y' ? true : false;
            this.txtOper.Text = entity.Oper;
            this.txtDate.Text = entity.DATE1.ToString();
            this.txtSL0.Text = entity.SL0.ToString();
            this.txtYcsl.Text = entity.ycsl.ToString();
            this.txtYcje.Text = entity.ycje.ToString();
            this.txtSl2.Text = entity.SL2.ToString();
            this.txtJe2.Text = entity.JE2.ToString();
            this.txtSl3.Text = entity.SL3.ToString();
            this.txtJe3.Text = entity.JE3.ToString();
            this.txtSuppID.Text = entity.SUPP_ID;
            this.txtDate1.Text = entity.date_1.ToString();
            this.txtDateLast.Text = entity.date_last.ToString();
            this.txtSlLast.Text = entity.SL_last.ToString();
            this.txtJeLast.Text = entity.Je_last.ToString();
            this.txtDate0.Text = entity.date_0.ToString();
            this.txtBats.Text = entity.BATS.ToString();
            this.txtBat.Text = entity.BAT.ToString();
        }

        protected void ChangeButton(bool enable)
        {
            this.btnAdd.Enabled = enable;
            this.btnDel.Enabled = enable;
            this.btnMod.Enabled = enable;
            this.btnSek.Enabled = enable;
            this.btnPrint.Enabled = enable;
            this.btnRef.Enabled = enable;
            this.btnQuit.Enabled = enable;
            this.btnOk.Enabled = !enable;
            this.btnCan.Enabled = !enable;
        }
    }
}
